<?php

namespace app\models;

use app\librarys\DbCacheUtility;

/**
 * 权限
 * Class Permission
 * @package app\models
 */
class Permission
{
    public static function hasPermission($controllerId, $actionId)
    {
        $currentManager = Manager::getCurrentManager();
        $hasPermission = false;
        if ($currentManager != null) {
            if ($currentManager['id'] == '0') {  // 系统超级管理员
                $hasPermission = true;
            } else {
                if (isset($controllerId) && isset($actionId)) {

                    $noAuthActions = self::_noAuthActions();
                    foreach ($noAuthActions as $noAuthAction) {
                        if ($noAuthAction['controller_id'] == $controllerId && $noAuthAction['action_id'] == $actionId) {
                            $hasPermission = true;
                            break;
                        }
                    }

                    if ($hasPermission == false) {
                        foreach ($currentManager['permission'] as $item) {
                            if ($item['controller_id'] == $controllerId && $item['action_id'] == $actionId) {
                                $hasPermission = true;
                                break;
                            }
                        }
                    }

                }
            }
        }
        return $hasPermission;
//        return true;
    }

    /**
     * 无需鉴权即可访问的action
     * @return array
     */
    private static function _noAuthActions()
    {
        return [
            ['controller_id' => 'system', 'action_id' => 'person-info-partview'],  // 管理员个人信息编辑修改
            ['controller_id' => 'system', 'action_id' => 'modify-password-handler'],  // 管理员对各人的密码进行修改handler
            ['controller_id' => 'system', 'action_id' => 'modify-manager-info-handler'],  // 管理员个人信息编辑修改handler
            ['controller_id' => 'common', 'action_id' => 'department-tree-handler'],  // 部门树
        ];
    }

    public static function isSystemAdmin()
    {
        $currentManager = Manager::getCurrentManager();
        if ($currentManager['institution_code'] == '0') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断当前登录manager是否对institutionCode的机构有访问权限
     * @param string $institutionCode
     * @return bool
     */
    public static function hasInstitutionPermission($institutionCode)
    {
        $currentInstitutionCode = Manager::getCurrentManager()['institution_code'];

        if ($currentInstitutionCode == '0') {
            return true;
        }

        if ($institutionCode == $currentInstitutionCode) {
            return true;
        }

        $hasPermission = false;
        do {
            $cmd = \Yii::$app->db->createCommand('SELECT parent_code FROM t_institution WHERE `code`=:institutionCode');
            $cmd->bindValue(':institutionCode', $institutionCode);
            $parentCode = DbCacheUtility::queryScalar($cmd, 600);
            if ($parentCode == $currentInstitutionCode) {
                $hasPermission = true;
            } else {
                $institutionCode = $parentCode;
            }
        } while ($institutionCode != '0' && $hasPermission == false && $institutionCode != false);

        return $hasPermission;
    }
}